import 'dart:ui';
import 'package:fijkplayer/fijkplayer.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:h_flutter_119/page/detail_page/widget/VideoListItemWidget.dart';
import 'package:h_flutter_119/utils/log_utils.dart';
import 'package:h_flutter_119/widget/fijkplayer_skin/fijkplayer_skin.dart';

import '../../widget/fijkplayer_skin/video_config.dart';

class EyeDetailPage extends StatefulWidget {
  Map<String, dynamic> _map;

  EyeDetailPage(this._map, {Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() => _EyeDetailPageState();
}

class _EyeDetailPageState extends State<EyeDetailPage> {
  FijkPlayer _fijkPlayer = FijkPlayer();
  ShowConfigAbs videoConfig = PlayerShowConfig();
  String mTitle = '';
  String _currentPlayUrl = "";
  Map<String, dynamic> _map = Map();

  @override
  void initState() {
    super.initState();

    setState(() {
      _map = widget._map;
    });
    _fijkPlayer.setDataSource(_map['playUrl'], autoPlay: true);

    // LogE('_playUrl =${_map['playUrl']}');
    // setState((){
    //   _currentPlayUrl = _map['playUrl'];
    //   _fijkPlayer.setDataSource(_currentPlayUrl,autoPlay: true);
    // });
  }

  @override
  void dispose() {
    super.dispose();
    _fijkPlayer.release();
  }

  @override
  void didUpdateWidget(EyeDetailPage oldWidget) {
    LogE('didUpdateWidget oldWidget=${oldWidget}');
  }

  @override
  Widget build(BuildContext context) {
    return HPlayer(context);

    /*return Column(
      children: [
        Container(
            height: MediaQueryData.fromWindow(window).padding.top,
            color: Colors.black),
        Container(
          height: 450.w,
          color: Colors.black,
          child: Stack(
            children: [
              Positioned(
                  left: 0,
                  top: 0,
                  right: 0,
                  bottom: 0,
                  child: Hero(
                      tag: _map['videoId'],
                      child: Image.network(
                        _map['coverUrl'],
                        fit: BoxFit.cover,
                      ))),
              FijkView(
                player: _fijkPlayer,
                height: 450.w,
                color: Colors.black,
                fit: FijkFit.cover,
                panelBuilder: (FijkPlayer player, FijkData data,
                    BuildContext context, Size viewSize, Rect texturePos) {
                  return CustomFijkPanel(
                    player: player,
                    viewSize: viewSize,
                    texturePos: texturePos,
                    showConfig: videoConfig,
                    curPlayUrl: _currentPlayUrl,
                    playerTitle: mTitle,
                    pageContent: context,
                  );
                },
              )
            ],
          ),
        ),
        Expanded(
            child: MediaQuery.removePadding(
                removeTop: true,
                context: context,
                child: VideoListItemWidget(
                  (title, videoUrl) async {
                    setState(() {
                      mTitle = title;
                    });
                    await _fijkPlayer.reset();
                    _fijkPlayer.setDataSource(videoUrl, autoPlay: true);
                  },
                )))
      ],
    );*/
  }

  Widget HPlayer(BuildContext context) {
    return Scaffold(
      body: Container(
        child: Column(
          children: [
          Container(height: MediaQueryData.fromWindow(window).padding.top,
          color: Colors.black,),
            Container(
              height: 450.w,
              color: Colors.black,
              child: Stack(
                children: [
                  Positioned(
                      left: 0,
                      top: 0,
                      right: 0,
                      bottom: 0,
                      child: Hero(
                          tag: _map['videoId'],
                          child: Image.network(
                            _map['coverUrl'],
                            fit: BoxFit.cover,
                            width: 450.w,
                          ),)
                  ),
                  FijkView(
                    height: 450.w,
                    fit: FijkFit.cover,
                    player: _fijkPlayer,
                  )
                ],
              ),
            ),
            Expanded(
                child: MediaQuery.removePadding(
                    removeTop: true,
                    context: context,
                    child: VideoListItemWidget(
                      _map['videoId']??"".toString(),
                      (title, videoUrl) async {
                        setState(() {
                          mTitle = title;
                        });
                        await _fijkPlayer.reset();
                        _fijkPlayer.setDataSource(videoUrl, autoPlay: true);
                      },
                    )))
          ],
        ),
      ),
    );
  }
}
